package com.bx.imserver.rabbitmq;

import com.bx.imcommon.RabbitConstant;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: RabbitConfig
 * @Description:
 * Broker:它提供一种传输服务,它的角色就是维护一条从生产者到消费者的路线，保证数据能按照指定的方式进行传输,
 * Exchange：消息交换机,它指定消息按什么规则,路由到哪个队列。
 * Queue:消息的载体,每个消息都会被投到一个或多个队列。
 * Binding:绑定，它的作用就是把exchange和queue按照路由规则绑定起来.
 * Routing Key:路由关键字,exchange根据这个关键字进行消息投递。
 * vhost:虚拟主机,一个broker里可以有多个vhost，用作不同用户的权限分离。
 * Producer:消息生产者,就是投递消息的程序.
 * Consumer:消息消费者,就是接受消息的程序.
 * Channel:消息通道,在客户端的每个连接里,可建立多个channel.
 * @Author guochuang
 * @Date 2021/10/25
 * @Version 1.0
 */
@Configuration
public class RabbitConfig {

    /**
     * 功能描述: 序列化
     *
     * @param connectionFactory
     * @return org.springframework.amqp.rabbit.core.RabbitTemplate
     * @author zhouwenjie
     * @date 2022/5/6 1:11
     */
    @Bean
    public RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory) {
        Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(converter);
        return rabbitTemplate;
    }

    /**
     * 功能描述: 反序列化
     *
     * @param
     * @return org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory
     * @author zhouwenjie
     * @date 2022/5/6 1:11
     */
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(SimpleRabbitListenerContainerFactoryConfigurer factoryConfigurer, ConnectionFactory connectionFactory){
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        factoryConfigurer.configure(factory,connectionFactory);
        return factory;
    }


    /**
     * 针对消费者配置
     * 1. 设置交换机类型
     * 2. 将队列绑定到交换机
     * FanoutExchange: 将消息分发到所有的绑定队列，无routingkey的概念
     * HeadersExchange ：通过添加属性key-value匹配
     * DirectExchange:按照routingkey分发到指定队列
     * TopicExchange:多关键字匹配
     */
//    @Bean
//    public TopicExchange topicExchange() {
//        return new TopicExchange(EXCHANGE_A, true, false);
//    }
//    @Bean
//    public FanoutExchange fanoutExchangeGroupA() {
//        return new FanoutExchange(EXCHANGE_A, true, false);
//    }
//    @Bean
//    public FanoutExchange fanoutExchangeGroupB() {
//        return new FanoutExchange(EXCHANGE_B, true, false);
//    }
    @Bean
    public FanoutExchange fanoutExchangePrivateC() {
        return new FanoutExchange(RabbitConstant.EXCHANGE_C, true, false);
    }
//    @Bean
//    public FanoutExchange fanoutExchangePrivateD() {
//        return new FanoutExchange(EXCHANGE_D, true, false);
//    }
    /**
     * 获取队列A
     *
     * @return
     */
    @Bean
    public Queue queueGroupA() {
        //队列持久
        return new Queue(RabbitConstant.QUEUE_A, true);
    }

    /**
     * 获取队列B
     *
     * @return
     */
    @Bean
    public Queue queueGroupB() {
        //队列持久
        return new Queue(RabbitConstant.QUEUE_B, true);
    }

    @Bean
    public Queue queueGroupC() {
        //队列持久
        return new Queue(RabbitConstant.QUEUE_C, true);
    }
    /**
     * 获取队列A
     *
     * @return
     */
    @Bean
    public Queue queueGroupD() {//队列持久
        return new Queue(RabbitConstant.QUEUE_D, true);
    }
    /**
     * 获取队列A
     *
     * @return
     */
    @Bean
    public Queue queuePrivateE() {//队列持久
        return new Queue(RabbitConstant.QUEUE_E, true);
    }
    /**
     * 获取队列A
     *
     * @return
     */
    @Bean
    public Queue queuePrivateF() {
        //队列持久
        return new Queue(RabbitConstant.QUEUE_F, true);
    }
    /**
     * 获取队列A
     *
     * @return
     */
    @Bean
    public Queue queuePrivateG() {
        //队列持久
        return new Queue(RabbitConstant.QUEUE_G, true);
    }
    /**
     * 获取队列A
     *
     * @return
     */
    @Bean
    public Queue queuePrivateH() {//队列持久
        return new Queue(RabbitConstant.QUEUE_H, true);
    }


    /**
     * 交换器绑定队列
     * @return
     */
//    @Bean
//    public Binding bindingFanoutExchangeA() {
//        return BindingBuilder.bind(queueGroupA()).to(fanoutExchangeGroupA());
//    }

    /**
     * 交换器绑定队列
     * @return
     */
//    @Bean
//    public Binding bindingFanoutExchangeB() {
//        return BindingBuilder.bind(queueGroupB()).to(fanoutExchangeGroupA());
//    }
    /**
     * 交换器绑定队列
     * @return
     */
//    @Bean
//    public Binding bindingFanoutExchangeC() {
//        return BindingBuilder.bind(queueGroupC()).to(fanoutExchangeGroupB());
//    }
    /**
     * 交换器绑定队列
     * @return
     */
//    @Bean
//    public Binding bindingFanoutExchangeD() {
//        return BindingBuilder.bind(queueGroupD()).to(fanoutExchangeGroupB());
//    }
    /**
     * 交换器绑定队列
     * @return
     */
    @Bean
    public Binding bindingFanoutExchangeE() {
        return BindingBuilder.bind(queuePrivateE()).to(fanoutExchangePrivateC());
    }
    /**
     * 交换器绑定队列
     * @return
     */
//    @Bean
//    public Binding bindingFanoutExchangeF() {
//        return BindingBuilder.bind(queuePrivateF()).to(fanoutExchangePrivateC());
//    }
    /**
     * 交换器绑定队列
     * @return
     */
//    @Bean
//    public Binding bindingFanoutExchangeG() {
//        return BindingBuilder.bind(queuePrivateG()).to(fanoutExchangePrivateD());
//    }
    /**
     * 交换器绑定队列
     * @return
     */
//    @Bean
//    public Binding bindingFanoutExchangeH() {
//        return BindingBuilder.bind(queuePrivateH()).to(fanoutExchangePrivateD());
//    }
    /**
     * 声明死信交换机
     *
     * @return
     */
//    @Bean
//    public Exchange deadExchange() {
//        return ExchangeBuilder.directExchange(DEAD_EXCHANGE).build();
//    }
//
//    /**
//     * 声明死信队列
//     *
//     * @return
//     */
//    @Bean
//    public Queue deadQueue() {
//        return QueueBuilder.durable(DEAD_QUEUE).build();
//    }


    /**
     * 绑定死信的队列和交换机
     *
     * @param deadExchange
     * @param deadQueue
     * @return
     */
//    @Bean
//    public Binding deadBind(Exchange deadExchange, Queue deadQueue) {
//        return BindingBuilder.bind(deadQueue).to(deadExchange).with(DEAD_ROUTING_KEY).noargs();
//    }

    /**
     * 声明交换机，同channel.exchangeDeclare(EXCHANGE, BuiltinExchangeType.DIRECT);
     *
     * @return
     */
//    @Bean
//    public Exchange bootExchange() {
//        return ExchangeBuilder.directExchange(EXCHANGE).build();
//    }

    /**
     * 声明队列，同channel.queueDeclare(QUEUE, true, false, false, null);
     * 绑定死信交换机及路由key
     *
     * @return
     */
//    @Bean
//    public Queue bootQueue() {
//        Map<String, Object> args = new HashMap<>(3);
//        //声明当前队列绑定的死信交换机
//        args.put("x-dead-letter-exchange", DEAD_EXCHANGE);
//        //声明当前队列的死信路由 key
//        args.put("x-dead-letter-routing-key", DEAD_ROUTING_KEY);
//        //声明队列的 TTL
//        args.put("x-message-ttl", 1000);
//        return QueueBuilder.durable(QUEUE).withArguments(args).build();
////        return QueueBuilder.durable(QUEUE)
////                .deadLetterExchange(DEAD_EXCHANGE)
////                .deadLetterRoutingKey(DEAD_ROUTING_KEY)
////                //声明队列属性有更改时需要删除队列
////                //给队列设置消息时长
////                .ttl(100000)
////                //队列最大长度
////                .maxLength(1)
////                .build();
//    }
//
//    /**
//     * 绑定队列和交换机,同 channel.queueBind(QUEUE, EXCHANGE, ROUTING_KEY);
//     *
//     * @param bootExchange
//     * @param bootQueue
//     * @return
//     */
//    @Bean
//    public Binding bootBind(Exchange bootExchange, Queue bootQueue) {
//        return BindingBuilder.bind(bootQueue).to(bootExchange).with(ROUTING_KEY).noargs();
//    }

}
